<?php
/*
 * 活动类
 * 公众号 操作暂时也在此文件里
 */
namespace Home\Logic;

class ActionLogic{
    
    CONST ROUGH_STATUS = 2;
    CONST RELEASE_STATUS = 1;
    CONST DELETE_STATUS=4;
    
    public function __construct() {
        $this->a = D('Action');
    }
    
    public function getStore(){
        $s = D('Store');
        return $s->where(array('user_id'=>$_SESSION['id']))->find();       
    }
    
    public function getAction($id){
        return $this->a->where(array('id'=>$id))->find();
    }
    
    public function getActionsByStoreId($store_id,$type=0,$start,$length=12){
        if($store_id == NULL || count($store_id)<1){
            return ;
        }
        $where[] = 'store_id in('.implode(',',(array)$store_id).')';
        if($type>0){
            $where[] = "status=".$type;
        }else{
            $where[] = "status!=4 ";
        }
        return $this->a->where(implode(' and ',$where))->order('create_time desc')->limit($start,$length)->select();
    }
    
    public function getActionByStoreId($store_id){
        return $this->a->where(array('store_id'=>$store_id))->order('create_time desc')->select();
    }
    
    public function getMarketingModel(){
        $m = D('Marketing_model');
        return $m->where(array('status'=>1))->select();
    }
    
    public function insertAction($arr){
        $id = $this->a->add($arr);
        return $id;
    }
    
    public function updateAction($arr){
        return $this->a->where('id='.$arr['id'])->save($arr);
    }
    
    public function updateActionStatus($id,$status){
        $data['status'] = $status;
        return $this->a->where('id='.$id)->save($data);
    }
    
    /*移动端专用*/
    public function getActionList($store_id,$barber_id=0,$start=0,$length=12){
        $where = 'store_id='.$store_id.' and status=1'.' and end_time>'.time().' and barber_id in(0,'.$barber_id.')';
        return $this->a->where($where)->order('create_time desc')->limit($start,$length)->select();
    }

    //移动端 参加颜值
    public function insertFace($data){
        $f = D('Face');
        return $f->add($data);
    }

    public function selectFace($id){
        $f = D('Face');
        return $f->where('id='.$id)->find();
    }

    public function percentFace($score,$action_id){
        $f = D('Face');
        $action_id = 'action_id='.$action_id;
        $count = $f->where($action_id)->count();
        $where = 'score <='.$score.' and '.$action_id;
        $num = $f->where($where)->count();
        return array('count'=>$count,'num'=>$num);
    }
    //好友加成
    public function updateFace($id,$score){
        $f = D('Face');
        $arr['score'] = $score;
        return $f->where('id='.$id)->save($arr);
    }

    public function getFaceByAction($action_id){
        $f = D('Face');
        $arr = array('action_id='.$action_id);
        return $f->where($arr)->order('score desc')->select();
    }

    public function updateFaceMsg($id){
        $f = D('Face');
        $data['msg']=1;
        return $f->where('id='.$id)->save($data);
    }
    public function getFaceNumByAction($action_id){
        $f = D('Face');
        return $f->where('action_id='.$action_id)->count();
    }

}